<?php
class Con_idcard extends Acan{
	public $defaultAct='index';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
	}
	function Act_index(){
		$this->atitle = '身份证归属地查询';
		include $this->atpl();
	}
	function Act_get($m=false,$p=false){
		if($m){
			$idcard = $m;
		}else{
			header('Access-Control-Allow-Origin: *');
			$idcard = $_GET['m'];
		}
		$num = strlen($idcard);
		if($num!=6 && $num!=15 && $num!=18){
			$data->msg = '号码错误';
			$data->status = 0;
			if($p){return $data;}else{Afun_jsonp($data,true);}
		}
		$pcode = substr($idcard,0,6);
		$data = Afun_cache_get('Atool:pcode:'.$pcode);
		if(empty($data)){
			$data = Afun_db()->model(Acan_table_data_idcard)->ofind('idcard='.$pcode);
			if(!$data){
				$data = $this->guishu($idcard);
			}else{
				unset($data->id,$data->area);
				$data->status = 1;
			}
			unset($data->postcode,$data->areacode);
			$data->idcard = $idcard;
			Afun_cache_set('Atool:pcode:'.$pcode,$data,86400*7);
		}
		if($p){return $data;}else{Afun_jsonp($data,true);}
	}
	function Act_gets(){
		header('Access-Control-Allow-Origin: *');
		$idcards = $_GET['m'];
		$mrr = explode('.',$idcards);
		foreach($mrr as $k=>$v){
			$data[] = $this->Act_get($v,true);
		}
		Afun_jsonp($data,true);
	}
	function Act_guishu($idcard='330226198807252898'){
		$url = "http://www.cz88.net/tools/id.php";
		$curl = Afun_curl('post',$url,array('in_id'=>$idcard));
		preg_match_all('/<div class="data">(.+)<\/div>/',$curl['data'],$mrr);
		preg_match('/([0-9]+)/',$mrr[1][3],$arr);
		$card = $arr[1];
		if(strlen($card)==18){
			$data->area = $mrr[1][0];
		}
		$data->idcard = substr($card,0,6);
		return $data;
	}
	function Act_zheng(){
		return;
		$model = Afun_db()->model(Acan_table_data_phone);
		$con->where = 'city is null';
		$con->group = 'area';
		$con->limit = '0,100';
		$data = $model->ofindAll($con);
		echo '<table>';
		if($data)
		foreach($data as $k=>$v){
			$arr = explode('省',$v->area);
			$sdata = array();
			if(count($arr)==1){
				$len = strlen($arr[0]);
				if(in_array(substr($arr[0],0,9),array('内蒙古','重庆市'))){
					$sdata['province'] = substr($arr[0],0,9);
					if(substr($arr[0],0,9)=='重庆市'){
						$sdata['province'] = '重庆';
					}
					if(substr($arr[0],-3)=='市'){
						$sdata['city'] = substr($arr[0],9,-3);
					}else{
						$sdata['city'] = substr($arr[0],9);
					}
				}elseif(in_array(substr($arr[0],0,6),array('青海','西藏','新疆','宁夏'))){
					$sdata['province'] = substr($arr[0],0,6);
					if(substr($arr[0],-3)=='市'){
						$sdata['city'] = substr($arr[0],6,-3);
					}else{
						$sdata['city'] = substr($arr[0],6);
					}
				}elseif(substr($arr[0],-3)=='市'){
					$sdata['province'] = $sdata['city'] = substr($arr[0],0,-3);
				}
				$num = $model->updateAll($sdata,"area='".$v->area."' and city is null");
				echo '<tr><td>'.$v->area.'</td><td>变省:'.$sdata['province'].'</td><td>市:'.$sdata['city'].'</td><td>更新'.$num.'条</td></tr>';
			}elseif(count($arr)==2){
				$sdata['province'] = $arr[0];
				if(substr($arr[1],-3)=='市'){
					$sdata['city'] = substr($arr[1],0,-3);
				}else{
					$sdata['city'] = $arr[1];
				}
				$num = $model->updateAll($sdata,"area='".$v->area."' and city is null");
				echo '<tr><td>'.$v->area.'</td><td>变省:'.$sdata['province'].'</td><td>市:'.$sdata['city'].'</td><td>更新'.$num.'条</td></tr>';
			}
		}
		echo '</table>';
	}
}
?>